layui.define(['jquery', 'layer'], function(exports){

    function request(options) {

        let defaultOptions = {
            method: 'GET',
            dataType: 'json',
            contentType: 'application/x-www-form-urlencoded;charset=UTF-8'
        };

        options = $.extend({}, defaultOptions, options);

        return new Promise((resolve, reject) => {
            $.ajax(options)
                .done(resolve)
                .fail(function (err) {
                    // 请求失败,做统一处理
                    handleRequestError(err);

                    reject(err);
                });
        })

    }

    function handleRequestError(err) {
        layer.msg(err.responseText || '获取数据失败', {icon: 2});
    }

    exports('request', request);

});
